﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data.OracleClient;
using System.Configuration;

namespace CRM.Web.ReportManage
{
    public partial class CaseCallbackInfo : Common.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageHelper.SetPopCalender(this.Page, txtBeginDate);
                PageHelper.SetPopCalender(this.Page, txtBeginTime, "HH:mm:ss");
                PageHelper.SetPopCalender(this.Page, txtEndDate);
                PageHelper.SetPopCalender(this.Page, txtEndTime, "HH:mm:ss");

                ShowReportHeader();
                ddlCaseTypeBinding();
                ddlBusinessBinding();
                ddlBusiness_SelectedIndexChanged(ddlBusiness, null);

                ReportHelper.myReportEnum.pConnectString = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
            }
        }

        #region 数据绑定

        /// <summary>
        /// 事业部选项加载
        /// </summary>
        private void ddlBusinessBinding()
        {
            string pSql = "";
            T_BUSINESSDIVISION_BLL dal = new T_BUSINESSDIVISION_BLL();
            if (!UserLoginInfo.IsAdministrator())
            {
                pSql = String.Format(" AND BUSINESSDIVISIONCODE = '{0}' ORDER BY BUSINESSDIVISIONNAME", _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE);
                List<T_BUSINESSDIVISION_Entity> list = dal.GetList(pSql);
                ddlBusiness.DataSource = list;
                ddlBusiness.DataTextField = "BUSINESSDIVISIONNAME";
                ddlBusiness.DataValueField = "BUSINESSDIVISIONCODE";
                ddlBusiness.DataBind();
                ddlBusiness.AutoPostBack = true;
            }
            else
            {
                pSql = " AND STATUS = 0 ORDER BY BUSINESSDIVISIONNAME";
                List<T_BUSINESSDIVISION_Entity> list = dal.GetList(pSql);
                ddlBusiness.DataSource = list;
                ddlBusiness.DataTextField = "BUSINESSDIVISIONNAME";
                ddlBusiness.DataValueField = "BUSINESSDIVISIONCODE";
                ddlBusiness.DataBind();
                ddlBusiness.AutoPostBack = true;
                ddlBusiness.Items.Insert(0, new ListItem("全部", ""));
            }
        }

        /// <summary>
        /// 选择列表数据加载
        /// </summary>
        /// <param name="Filter"></param>
        private void gvConditionBinding(string code)
        {
            string pSql = "";
            if (code != "")
            {
                pSql = String.Format(" AND DICTTYPE = '{0}' AND BUSINESSDIVISIONCODE = '{1}' ORDER BY BUSINESSDIVISIONNAME", "VisitSituation", code);
            }
            else
            {
                pSql = String.Format(" AND DICTTYPE = '{0}' ORDER BY BUSINESSDIVISIONNAME", "VisitSituation"); ;
            }

            DataTable dt = new DataTable();
            T_DICTIONARY_BLL dal = new T_DICTIONARY_BLL();
            dt = dal.GetDataTable(pSql);

            gvCondition.DataSource = dt;
            gvCondition.DataKeyNames = new string[] { "DICTKEY", "BUSINESSDIVISIONCODE", "BUSINESSDIVISIONNAME" };
            gvCondition.DataBind();
        }

        protected void ddlBusiness_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvConditionBinding(ddlBusiness.SelectedValue);
        }

        /// <summary>
        /// 统计工单类型
        /// </summary>
        private void ddlCaseTypeBinding()
        {
            ddlCaseType.Items.Clear();
            EnumHelper.FillListControl(ddlCaseType, typeof(ReportCaseType));
            ddlCaseType.Items.Insert(0, new ListItem("", ""));
        }

        protected void ckbVisible_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbVisible.Checked)
                this.gvCondition.Enabled = false;
            else
                this.gvCondition.Enabled = true;
        }

        /// <summary>
        /// 显示统计报表样式
        /// </summary>
        private void ShowReportHeader()
        {
            #region 加载报表样式

            ReportHelper.myReportEnum.pLastTable = new DataTable();
            ReportHelper.myReportEnum.pLastTable.Clear();
            ReportHelper.myReportEnum.pLastTable.Columns.Clear();

            ReportHelper.myReportEnum.pLastTable.Columns.Add("事业部");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("工单类型");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("回访情况");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("工单数量");

            ReportHelper.myReportEnum.pLastTable.Columns.Add("24小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("24小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("48小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("48小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("72小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("72小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时以上回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时以上回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("未回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("未回访率");

            int index = 0;
            DataRow my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();
            my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            my_dr[index++] = "";
            ReportHelper.myReportEnum.pLastTable.Rows.Add(my_dr);

            this.gvReport.DataSource = ReportHelper.myReportEnum.pLastTable;
            this.gvReport.DataBind();

            #endregion
        }
        #endregion

        #region GridView 事件
        /// <summary>
        /// 行创建事件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvReport_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (TableCell tc in e.Row.Cells)
            {
                tc.Wrap = false;
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                gvReport.Caption = String.Format("工单回访情况报表：事业部:{0},统计开始时间:{1}----统计结束时间:{2}", ReportHelper.myReportEnum.pBusinessName, ReportHelper.myReportEnum.SDate_DateTime, ReportHelper.myReportEnum.EDate_DateTime);
    
                //GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                //rowHeader.BackColor = System.Drawing.Color.White;
                //rowHeader.Font.Bold = true;

                //TableCellCollection cells = e.Row.Cells;
                //TableCell headerCell = new TableCell();
                //headerCell.Text = String.Format("工单回访情况报表：事业部:{0},统计开始时间:{1}----统计结束时间:{2}", ReportHelper.myReportEnum.pBusinessName, ReportHelper.myReportEnum.SDate_DateTime, ReportHelper.myReportEnum.EDate_DateTime);
                //headerCell.ColumnSpan = cells.Count;
                //headerCell.HorizontalAlign = HorizontalAlign.Left;
                //rowHeader.Cells.Add(headerCell);
                ////rowHeader.Cells.Add(headerCell);
                //rowHeader.Visible = true;
                //gvReport.Controls[0].Controls.AddAt(0, rowHeader);
            }
        }

        /// <summary>
        /// 行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //e.Row.Attributes.Add("style", "background:#FFF");
        }

        protected void gvCondition_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                (e.Row.Cells[0].FindControl("cbConditionHeader") as CheckBox).Attributes.Add("click", "checkAll(this)");
            }
        }

        protected void cbConditionHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in gvCondition.Rows)
            {
                (gvr.Cells[0].FindControl("cbCondition") as CheckBox).Checked = (sender as CheckBox).Checked;
            }
        }

        #endregion

                /// <summary>
        /// 开始查询操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            #region 选择条件判断

            if (txtBeginDate.Text.Trim() == "" || txtEndDate.Text.Trim() == "")
            {
                PageHelper.ShowMessage("请选择开始日期或结束日期！");
                return;
            }

            if (this.txtBeginTime.Text.Trim() == "" && txtEndTime.Text.Trim() == "") { }
            else if (this.txtBeginTime.Text.Trim() != "" && txtEndTime.Text.Trim() != "") { }
            else
            {
                PageHelper.ShowMessage("请选择开始时间或结束时间！");
                return;
            }

            if (((TimeSpan)DateTime.Parse(txtEndDate.Text).Date.Subtract(DateTime.Parse(txtBeginDate.Text).Date)).Ticks < 0)
            {
                PageHelper.ShowMessage("开始日期不能大于结束日期！");
                return;
            }

            if (ddlCaseType.SelectedItem.Text == "")
            {
                PageHelper.ShowMessage("请选择要统计的工单类型！");
                return;
            }
            #endregion

            #region 生成时间参数

            ReportHelper.myReportEnum.pBusinessName = this.ddlBusiness.SelectedItem.Text;
            ReportHelper.myReportEnum.pBusinessCode = this.ddlBusiness.SelectedValue;

            ReportHelper.myReportEnum.SDate_Varchar = DateTime.Parse(txtBeginDate.Text).ToString("yyyyMMdd");
            ReportHelper.myReportEnum.EDate_Varchar = DateTime.Parse(txtEndDate.Text).ToString("yyyyMMdd");

            if (txtBeginTime.Text != "")
                ReportHelper.myReportEnum.SDate_DateTime = DateTime.Parse(txtBeginDate.Text).ToString("yyyy-MM-dd") + " " + DateTime.Parse(txtBeginTime.Text).ToString("HH:mm:ss");
            else
                ReportHelper.myReportEnum.SDate_DateTime = DateTime.Parse(txtBeginDate.Text).ToString("yyyy-MM-dd 00:00:00");

            if (txtEndTime.Text != "")
                ReportHelper.myReportEnum.EDate_DateTime = DateTime.Parse(txtEndDate.Text).ToString("yyyy-MM-dd") + " " + DateTime.Parse(txtEndTime.Text).ToString("HH:mm:ss");
            else
                ReportHelper.myReportEnum.EDate_DateTime = DateTime.Parse(txtEndDate.Text).ToString("yyyy-MM-dd 23:59:59");

            #endregion

            ReportHelper.myReportEnum.pBusinessName = this.ddlBusiness.SelectedItem.Text;
            ReportHelper.myReportEnum.pBusinessCode = this.ddlBusiness.SelectedValue;
            ReportHelper.myReportEnum.pCaseName = this.ddlCaseType.SelectedItem.Text;

            SetReportEnumZero();

            ReportByCondition();
        }

        /// <summary>
        /// 按工单类型统计
        /// </summary>
        private void ReportByCondition()
        {
            #region 加载报表抬头

            ReportHelper.myReportEnum.pLastTable = new DataTable();
            ReportHelper.myReportEnum.pLastTable.Clear();
            ReportHelper.myReportEnum.pLastTable.Columns.Clear();

            ReportHelper.myReportEnum.pLastTable.Columns.Add("事业部");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("工单类型");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("回访情况");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("工单数量");

            ReportHelper.myReportEnum.pLastTable.Columns.Add("24小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("24小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("48小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("48小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("72小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("72小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时内回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时以上回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("120小时以上回访率");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("未回访数量");
            ReportHelper.myReportEnum.pLastTable.Columns.Add("未回访率");
            
            #endregion

            int index = 0;
            int count = gvCondition.Rows.Count, i;
            string strDictName = "";
            DataRow my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();

            if (ckbVisible.Checked)
            {
                #region 只统计汇总信息

                DataByReport(ReportHelper.myReportEnum.pCaseName, ReportHelper.myReportEnum.pBusinessCode, "");

                index = 0;

                #region 加载报表数据

                my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();

                my_dr[index++] = ReportHelper.myReportEnum.pBusinessName;//事业部
                my_dr[index++] = ReportHelper.myReportEnum.pCaseName;//工单类型 
                my_dr[index++] = strDictName;//回访情况
                my_dr[index++] = ReportHelper.myCaseEnum.nCaseCount;//工单数量

                my_dr[index++] = ReportHelper.myCaseEnum.n24CallBack;//24小时内回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n24CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                my_dr[index++] = ReportHelper.myCaseEnum.n48CallBack;//48小时内回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n48CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                my_dr[index++] = ReportHelper.myCaseEnum.n72CallBack;//72小时内回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n72CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                my_dr[index++] = ReportHelper.myCaseEnum.n120CallBack;//120小时内回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n120CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                my_dr[index++] = ReportHelper.myCaseEnum.nOver120CallBack;//120小时以上回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.nOver120CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                my_dr[index++] = ReportHelper.myCaseEnum.nUnCallBack;//未回访数量
                my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.nUnCallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";

                ReportHelper.myReportEnum.pLastTable.Rows.Add(my_dr);

                #endregion

                ReportHelper.myCaseEnum.dtCaseCount += ReportHelper.myCaseEnum.nCaseCount;
                ReportHelper.myCaseEnum.dt24CallBack += ReportHelper.myCaseEnum.n24CallBack;
                ReportHelper.myCaseEnum.dt48CallBack += ReportHelper.myCaseEnum.n48CallBack;
                ReportHelper.myCaseEnum.dt72CallBack += ReportHelper.myCaseEnum.n72CallBack;
                ReportHelper.myCaseEnum.dt120CallBack += ReportHelper.myCaseEnum.n120CallBack;
                ReportHelper.myCaseEnum.dtOver120CallBack += ReportHelper.myCaseEnum.nOver120CallBack;
                ReportHelper.myCaseEnum.dtUnCallBack += ReportHelper.myCaseEnum.nUnCallBack;

                #endregion
            }
            else
            {
                for (i = 0; i < count; i++)
                {
                    if (((CheckBox)gvCondition.Rows[i].FindControl("cbCondition")).Checked == false) continue;

                    strDictName = gvCondition.Rows[i].Cells[1].Text;

                    if (ddlBusiness.SelectedItem.Text == "全部")
                    {
                        ReportHelper.myReportEnum.pBusinessName = gvCondition.DataKeys[i].Values["BUSINESSDIVISIONNAME"].ToString();
                        ReportHelper.myReportEnum.pBusinessCode = gvCondition.DataKeys[i].Values["BUSINESSDIVISIONCODE"].ToString();
                    }

                    DataByReport(ReportHelper.myReportEnum.pCaseName, ReportHelper.myReportEnum.pBusinessCode, strDictName);

                    index = 0;

                    #region 加载报表数据

                    my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();

                    my_dr[index++] = ReportHelper.myReportEnum.pBusinessName;//事业部
                    my_dr[index++] = ReportHelper.myReportEnum.pCaseName;//工单类型 
                    my_dr[index++] = strDictName;//回访情况
                    my_dr[index++] = ReportHelper.myCaseEnum.nCaseCount;//工单数量

                    my_dr[index++] = ReportHelper.myCaseEnum.n24CallBack;//24小时内回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n24CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                    my_dr[index++] = ReportHelper.myCaseEnum.n48CallBack;//48小时内回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n48CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                    my_dr[index++] = ReportHelper.myCaseEnum.n72CallBack;//72小时内回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n72CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                    my_dr[index++] = ReportHelper.myCaseEnum.n120CallBack;//120小时内回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.n120CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                    my_dr[index++] = ReportHelper.myCaseEnum.nOver120CallBack;//120小时以上回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.nOver120CallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";
                    my_dr[index++] = ReportHelper.myCaseEnum.nUnCallBack;//未回访数量
                    my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.nUnCallBack, ReportHelper.myCaseEnum.nCaseCount) + "%";

                    ReportHelper.myReportEnum.pLastTable.Rows.Add(my_dr);

                    #endregion

                    ReportHelper.myCaseEnum.dtCaseCount += ReportHelper.myCaseEnum.nCaseCount;
                    ReportHelper.myCaseEnum.dt24CallBack += ReportHelper.myCaseEnum.n24CallBack;
                    ReportHelper.myCaseEnum.dt48CallBack += ReportHelper.myCaseEnum.n48CallBack;
                    ReportHelper.myCaseEnum.dt72CallBack += ReportHelper.myCaseEnum.n72CallBack;
                    ReportHelper.myCaseEnum.dt120CallBack += ReportHelper.myCaseEnum.n120CallBack;
                    ReportHelper.myCaseEnum.dtOver120CallBack += ReportHelper.myCaseEnum.nOver120CallBack;
                    ReportHelper.myCaseEnum.dtUnCallBack += ReportHelper.myCaseEnum.nUnCallBack;
                }
            }

            index = 0;
            my_dr = ReportHelper.myReportEnum.pLastTable.NewRow();
            my_dr[index++] = "";
            my_dr[index++] = ReportHelper.myReportEnum.pCaseName;//工单类型 
            my_dr[index++] = "合计";

            my_dr[index++] = ReportHelper.myCaseEnum.dtCaseCount;//工单数量  
            my_dr[index++] = ReportHelper.myCaseEnum.dt24CallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dt24CallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";
            my_dr[index++] = ReportHelper.myCaseEnum.dt48CallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dt48CallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";
            my_dr[index++] = ReportHelper.myCaseEnum.dt72CallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dt72CallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";
            my_dr[index++] = ReportHelper.myCaseEnum.dt120CallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dt120CallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";
            my_dr[index++] = ReportHelper.myCaseEnum.dtOver120CallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dtOver120CallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";
            my_dr[index++] = ReportHelper.myCaseEnum.dtUnCallBack;
            my_dr[index++] = ReportHelper.AccountRate(ReportHelper.myCaseEnum.dtUnCallBack, ReportHelper.myCaseEnum.dtCaseCount) + "%";

            ReportHelper.myReportEnum.pLastTable.Rows.Add(my_dr);

            this.gvReport.DataSource = ReportHelper.myReportEnum.pLastTable;
            this.gvReport.DataBind();
        }

        private void DataByReport(string reportObject, string businessCode, string primaryKey)
        {
            StringBuilder pSql_Repair = new StringBuilder();
            StringBuilder pSql_Complaint = new StringBuilder();
            pSql_Repair.Append(String.Format("SELECT COUNT(*) AS COUNT FROM T_REPAIRCASE WHERE CREATETIME BETWEEN TO_DATE('{0}','yyyy-MM-dd HH24:mi:ss') AND TO_DATE('{1}','yyyy-MM-dd HH24:mi:ss') ", ReportHelper.myReportEnum.SDate_DateTime, ReportHelper.myReportEnum.EDate_DateTime));
            pSql_Complaint.Append(String.Format("SELECT COUNT(*) AS COUNT FROM T_COMPLAINTCASE WHERE CREATETIME BETWEEN TO_DATE('{0}','yyyy-MM-dd HH24:mi:ss') AND TO_DATE('{1}','yyyy-MM-dd HH24:mi:ss') ", ReportHelper.myReportEnum.SDate_DateTime, ReportHelper.myReportEnum.EDate_DateTime));

            if (businessCode != "")
            {
                pSql_Repair.Append(String.Format(" AND BUSINESSDIVISIONCODE = '{0}' ", businessCode));
                pSql_Complaint.Append(String.Format(" AND BUSINESSDIVISIONCODE = '{0}' ", businessCode));
            }
            if (!ckbVisible.Checked)
            {
                pSql_Repair.Append(String.Format(" AND VISITSITUATION = '{0}' ", primaryKey));
                pSql_Complaint.Append(String.Format(" AND VISITSITUATION = '{0}' ", primaryKey));
            }

            OracleConnection myOrc = new OracleConnection(ReportHelper.myReportEnum.pConnectString);
            OracleCommand myCmd = new OracleCommand();
            myCmd.Connection = myOrc;
            myOrc.Open();
            OracleDataReader myOraReader;

            if (reportObject == "报修工单")
            {
                myCmd.CommandText = pSql_Repair.ToString();
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nCaseCount = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 1 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n24CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 2 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n48CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 3 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n72CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 5 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n120CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) > 5 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nOver120CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Repair.ToString() + " AND FIRSTVISITTIME IS NULL ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nUnCallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();
            }
            else if (reportObject == "投诉工单")
            {
                myCmd.CommandText = pSql_Complaint.ToString();
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nCaseCount = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 1 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n24CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 2 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n48CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 3 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n72CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) <= 5 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.n120CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND ( FIRSTVISITTIME - PROCESSINGCOMPLETEDTIME ) > 5 ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nOver120CallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();

                myCmd.CommandText = pSql_Complaint.ToString() + " AND FIRSTVISITTIME IS NULL ";
                myOraReader = myCmd.ExecuteReader();
                if (myOraReader.Read())
                {
                    ReportHelper.myCaseEnum.nUnCallBack = StringHelper.ToInt(myOraReader["COUNT"].ToString());
                }
                myOraReader.Close();
            }
            myOrc.Close();
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ReportHelper.myReportEnum.pLastTable== null)
            {
                PageHelper.ShowMessage(this.Page, "没有数据可导出！");
                return;
            }

            if (ReportHelper.myReportEnum.pLastTable.Rows.Count >= 1)
            {
                Page.Response.Clear();
                Page.Response.Buffer = true;
                Page.Response.Charset = "GB2312";
                Page.Response.ContentEncoding = System.Text.Encoding.UTF8;

                Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(String.Format("工单回访情况报表_{0}", DateTime.Now.ToString("yyyyMMddHHmmss")), System.Text.Encoding.UTF8) + ".xls");
                Page.Response.AddHeader("Content-type", "application");
                Page.Response.ContentType = "application/ms-excel";

                this.EnableViewState = false;
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                this.gvReport.RenderControl(oHtmlTextWriter);
                Page.Response.Output.Write(oStringWriter.ToString());
                Page.Response.Flush();
                Page.Response.End();
                Page.Response.Close();
            }
            else
            {
                PageHelper.ShowMessage(this.Page, "没有数据可导出！");
            }
        }

        /// <summary>
        /// 导出Excel不报错的方法
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }

        /// <summary>
        /// 统计参数重置为零
        /// </summary>
        private void SetReportEnumZero()
        {
            ReportHelper.myCaseEnum.nCaseCount = 0;
            ReportHelper.myCaseEnum.n24CallBack = 0;
            ReportHelper.myCaseEnum.n48CallBack = 0;
            ReportHelper.myCaseEnum.n72CallBack = 0;
            ReportHelper.myCaseEnum.n120CallBack = 0;
            ReportHelper.myCaseEnum.nOver120CallBack = 0;
            ReportHelper.myCaseEnum.nUnCallBack = 0;

            ReportHelper.myCaseEnum.dtCaseCount = 0;
            ReportHelper.myCaseEnum.dt24CallBack = 0;
            ReportHelper.myCaseEnum.dt48CallBack = 0;
            ReportHelper.myCaseEnum.dt72CallBack = 0;
            ReportHelper.myCaseEnum.dt120CallBack = 0;
            ReportHelper.myCaseEnum.dtOver120CallBack = 0;
            ReportHelper.myCaseEnum.dtUnCallBack = 0;
        } 
    }
}
